using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.VisualBasic;
using Adge.Common;

namespace Adge.Web.UI.Controls
{

    [ValidationProperty("Text")]
    public partial class TextEditor : System.Web.UI.UserControl
    {
        // Fields        
        private HtmlEditorProvider RichTextEditor;
        private Unit _Height;
        private bool _HtmlEncode;
        private Unit _Width;
        private log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private const string ERROR_PREFIX = "Adge.Web.UI.Controls.TextEditor.ExceptionReporter:";
        // Methods
        public TextEditor()
        {
            base.Load += new EventHandler(this.Page_Load);
            base.Init += new EventHandler(this.Page_Init);            
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.RichTextEditor = new FckHtmlEditorProvider();
            this.RichTextEditor.ControlID = this.ID;
            this.RichTextEditor.Initialize();
        }

        private void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.PopulateLists();

                this.RichTextEditor.Width = this.Width;
                this.RichTextEditor.Height = this.Height;
                this.tblTextEditor.Width = this.Width.ToString();
                this.celTextEditor.Width = this.Width.ToString();

                this.plcEditor.Controls.Add(this.RichTextEditor.HtmlEditorControl);
                this.SetPanels();
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
            }
        }

        private string Decode(string strHtml)
        {
            if (this.HtmlEncode)
            {
                return this.Server.HtmlDecode(strHtml);
            }
            return strHtml;
        }

        private string Encode(string strHtml)
        {
            if (this.HtmlEncode)
            {
                return this.Server.HtmlEncode(strHtml);
            }
            return strHtml;
        }

        private string FormatHtml(string strText)
        {
            string left = strText;
            try
            {
                if (Operators.CompareString(left, "", false) != 0)
                {
                    left = Strings.Replace(left, "\r", "", 1, -1, CompareMethod.Binary);
                    left = Strings.Replace(left, "\n", "<br />", 1, -1, CompareMethod.Binary);
                }
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
            }
            return left;
        }

        private string FormatText(string strHtml)
        {
            string left = strHtml;
            try
            {
                if (string.Compare(left, "", false) != 0)
                {
                    left = HtmlUtils.StripWhiteSpace(left, true);
                    left = HtmlUtils.FormatText(left, false);
                }
            }
            catch (Exception ex)
            {
                log.Error(ERROR_PREFIX, ex);
            }
            return left;
        }

        private void PopulateLists()
        {

        }

        private void SetPanels()
        {

            this.pnlBasicTextBox.Visible = false;
            this.pnlRichTextBox.Visible = true;

        }

        public string DefaultMode
        {
            get
            {
                if (string.Compare(Conversions.ToString(this.ViewState["DefaultMode"]), "", false) == 0)
                {
                    return "RICH";
                }
                return Conversions.ToString(this.ViewState["DefaultMode"]);
            }
            set
            {
                if (string.Compare(value.ToUpper(), "BASIC", false) != 0)
                {
                    this.ViewState["DefaultMode"] = "RICH";
                }
                else
                {
                    this.ViewState["DefaultMode"] = "BASIC";
                }
            }
        }

        public Unit Height
        {
            get
            {
                return this._Height;
            }
            set
            {
                this._Height = value;
            }
        }

        public bool HtmlEncode
        {
            get
            {
                return this._HtmlEncode;
            }
            set
            {
                this._HtmlEncode = value;
            }
        }

        public HtmlEditorProvider RichText
        {
            get
            {
                return this.RichTextEditor;
            }
        }

        public string Text
        {
            get
            {                
                return this.Encode(this.RichTextEditor.Text);
            }
            set
            {
                if (string.Compare(value, "", false) != 0)
                {                    
                    this.RichTextEditor.Text = this.Decode(value);
                }
            }
        }

        public string TextRenderMode
        {
            get
            {
                return Conversions.ToString(this.ViewState["textrender"]);
            }
            set
            {
                string left = value.ToUpper().Substring(0, 1);
                if (((string.Compare(left, "R", false) != 0) & (string.Compare(left, "H", false) != 0)) & (string.Compare(left, "T", false) != 0))
                {
                    left = "H";
                }
                this.ViewState["textrender"] = left;
            }
        }

        public Unit Width
        {
            get
            {
                return this._Width;
            }
            set
            {
                this._Width = value;
            }
        }
    }
    
}